package com.leyou.lyelasticsearch.controller;

import com.leyou.common.entity.MyPage;
import com.leyou.item.entity.TbBrand;
import com.leyou.item.entity.TbSku;
import com.leyou.item.entity.TbSpuBo;
import com.leyou.item.entity.TbSpuDetail;
import com.leyou.lyelasticsearch.client.BrandClient;
import com.leyou.lyelasticsearch.client.CategoryClient;
import com.leyou.lyelasticsearch.client.GoodsClient;
import com.leyou.lyelasticsearch.client.GoodsFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsFeignClient goodsFeignClient;
    @GetMapping("page")
    public MyPage<TbSpuBo> page(){
        return goodsFeignClient.querySpuByKey(null, 1, 5, null);
    }

    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private CategoryClient categoryClient;

    @GetMapping("names")
    List<String> queryCategoryNamesByCids(@RequestParam("ids") List<Long> ids){
        return categoryClient.queryCategoryNamesByCids(ids);
    }

    //根据cid获取信息
    @GetMapping("cid/{cid}")
    public List<TbBrand> queryBrandByCid(@PathVariable("cid") Long cid){
        return brandClient.queryBrandByCid(cid);
    }

    //查询spu信息
    @GetMapping("/spu/page")
    public MyPage<TbSpuBo> querySpuByKey(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "5")int rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        return goodsClient.querySpuByKey(key, page, rows, saleable);
    }

    //根据spuId查询sku信息
    @GetMapping("list")
    public List<TbSku> querySkuByPid(Long id){
        return goodsClient.querySkusBySpuId(id);
    }


    //根据spuId获取spu详细信息
    @GetMapping("spu/detail/{id}")
    public TbSpuDetail querySpuDetailById(@PathVariable("id") Long id){
        return goodsClient.querySpuDetailById(id);
    }
}
